package com.huarui.initial.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.initial.model.Initial;
import com.huarui.initial.service.InitialService;
import com.huarui.initial.service.InitialSubmitService;
import com.huarui.initial.util.InitialUtil;
import com.huarui.initial.util.InitialVO;
import com.huarui.initialItem.model.InitialItem;
import com.huarui.initialItem.service.InitialItemService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.huarui.util.JsonUtil;
import com.opensymphony.xwork2.Action;


public class InitialAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	//private final static Logger logger = Logger.getLogger(InitialAction.class);
	private Integer id;
	private File excel;
	private InitialService initialService;
	private Integer initialId;
	private ListModel<InitialVO> listModel = new ListModel<InitialVO>();
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;
	private String data;
	private InitialItemService initialItemService;
	private InitialSubmitService initialSubmitService;
	private SessionMessage sm;
	private String initialJson;
	private String searchWordKey;
	private Map<String,Object> resultMap=new HashMap<String, Object>();
	

	/** 
	 * 查看表单
	 */
	public String getInitialById(){
		sm=UserHolder.getCurrentSessionMessage();
		resultMap = initialService.getInitialVOById(id,sm);			
		return Action.SUCCESS;
	}

	/** 
	 * 释放单据
	 */
	public String release() {
		sm = UserHolder.getCurrentSessionMessage();
		resultMap=initialService.release(id,sm);
		return Action.SUCCESS;
	}

	/** 
	 * 接手单据
	 */
	public String receive() {
		sm = UserHolder.getCurrentSessionMessage();
		resultMap=initialService.receive(id, sm);
		return Action.SUCCESS;
	}

	/** 
	 * 保存单据
	 */
	public String save() {
		sm = UserHolder.getCurrentSessionMessage();
		Initial initialtemp=(Initial) JsonUtil.getBean(data,Initial.class);		
		resultMap=initialService.save(initialtemp, sm);
		return Action.SUCCESS;
	}

	/** 
	 * 提交单据
	 */
	public String submit() {
		sm = UserHolder.getCurrentSessionMessage();
		Initial initialtemp=(Initial) JSONObject.toBean(JSONObject.fromObject(data),Initial.class);
		resultMap=initialSubmitService.sumbit(id, sm,initialtemp);
		return Action.SUCCESS;
	}
	
	/** 
	 * 删除
	 */
	public String delete() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=initialService.delete(id,sm);
		return Action.SUCCESS;
	}

	/** 
	 * 获得不合格零件数量
	 */
	public String countErrors() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=initialService.getCountErrorsByInitialId(id,sm);
		return Action.SUCCESS;
	}
	
	/**
	 * 查看期初列表
	 * @return 
	 * @throws IOException
	 */
	public String extList(){
	
		sm=UserHolder.getCurrentSessionMessage();
		listModel.setPageSize(limit == null ? 10 : limit);
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setOrderBy(sort==null?"gmtCreate":sort);
		if (dir != null){
			listModel.setDesc(dir.equals("DESC") ? true : false);
		}else{
			listModel.setDesc(true);
		}
		Map<String,Object> map=new HashMap<String,Object>();
		if(searchWordKey!=null){
			List<String> conditions = new ArrayList<String>();
			InitialUtil.addToCollection(conditions, InitialUtil.split(searchWordKey, " ,"));
			map.put("conditions",conditions);
		}
		resultMap=initialService.getInitialCountByCondition(sm, listModel,map);
		return Action.SUCCESS;
	}
	/**
	 * 读取excel数据
	 * @return 
	 * @throws IOException
	 */
	public String readExcelData() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=initialService.excelDataToDB(sm, initialId,excel);
		return Action.SUCCESS;
	}
	/**
	 * 期初明细保存
	 * @return 
	 * @throws IOException
	 */
	public String initialItemSave() {
		sm=UserHolder.getCurrentSessionMessage();
		Initial initial=(Initial) JSONObject.toBean(JSONObject.fromObject(initialJson), Initial.class);
		InitialItem initialItemTemp=(InitialItem) JSONObject.toBean(JSONObject.fromObject(data), InitialItem.class);
	    resultMap=initialItemService.saveInitialItem(initialItemTemp,initial,sm);
	    return Action.SUCCESS;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}

	public void setExcel(File excel) {
		this.excel = excel;
	}

	public void setInitialService(InitialService initialService) {
		this.initialService = initialService;
	}

	public void setInitialId(Integer initialId) {
		this.initialId = initialId;
	}

	public void setListModel(ListModel<InitialVO> listModel) {
		this.listModel = listModel;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public void setData(String data) {
		this.data = data;
	}

	public void setInitialItemService(InitialItemService initialItemService) {
		this.initialItemService = initialItemService;
	}

	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}

	public void setInitialJson(String initialJson) {
		this.initialJson = initialJson;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setInitialSubmitService(InitialSubmitService initialSubmitService) {
		this.initialSubmitService = initialSubmitService;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setSearchWordKey(String searchWordKey) {
		this.searchWordKey = searchWordKey;
	}
	

}